package cn.houserent.service;

import cn.houserent.dao.HouseDao;
import cn.houserent.pojo.House;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;

public class HouseBiz {

    private HouseDao houseDao = new HouseDao();

    public List<House> getHouseByTitle() {
        Transaction tx = null;
        List<House> houseList = null;

        try {
            tx = houseDao.currentSession().beginTransaction();  //开启事务
            houseList = houseDao.getHouseByTitle();
            tx.commit();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return houseList;
    }

    public List<House> findHousesByConditions(House conditions) {
        List<House> result = null;
        Transaction tx = null;
        try {
            tx = houseDao.currentSession().beginTransaction();
            // 准备hql，hql根据条件动态生成
            StringBuilder hql = new StringBuilder("from House where 1=1");
            // 判断联系人条件是否为空
            if (null != conditions.getContact()
                    && conditions.getContact().length() > 0)
                hql.append(" and contact = :contact");
            // 判断租金条件是否为空
            if (null != conditions.getPrice() && conditions.getPrice() > 0)
                hql.append(" and price < :price");
            // 判断发布日期条件是否为空
            if (null != conditions.getPubdate())
                hql.append(" and pubdate < :pubdate");
            result = houseDao.findHousesByConditions(hql.toString(), conditions);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return result;
    }


    /**
     *  分页查询信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    public List<House> findHouseByPage(int pageNo,int pageSize) {
        Transaction tx = null;
        List<House> houseList = null;

        try {
            tx = houseDao.currentSession().beginTransaction();      //开启事务
            houseList = houseDao.findHouseByPage(pageNo,pageSize);
            tx.commit();        //提交事务
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();      //回滚事务
            }
        }
        return houseList;
    }

    /**
     *  按房屋租金、标题、发布日期以及联系人分页查询房屋信息
     * @param sql
     * @param pageNo
     * @param pageSize
     * @param house
     * @return
     */
    public List<House> findHouseByPageInfo(int pageNo,int pageSize,House house) {
        Transaction tx = null;
        List<House> houseList = null;

        try {
            tx = houseDao.currentSession().beginTransaction();      //开启事务
            //HQL根据条件动态生成
            StringBuilder hql = new StringBuilder("from House where 1 = 1");
            if (house.getPrice() != null && house.getPrice() > 0) {
                hql.append(" and price>:price");
            }
            if (house.getTitle() != null && house.getTitle().length() > 0) {
                hql.append(" and title like :title");
            }
            if (house.getPubdate() != null) {
                hql.append(" and pubdate>:pubdate");
            }
            if (house.getContact() != null && house.getContact().length() > 0) {
                hql.append(" and contact like contact");
            }
            hql.append(" order by pubdate desc");
            houseList = houseDao.findHouseByPageInfo(hql.toString(),pageNo,pageSize,house);
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();      //回滚事务
            }
        }
        return houseList;
    }


    public Long houseCount() {
        Transaction tx = null;
        Long count = null;

        try {
            tx = houseDao.currentSession().beginTransaction();      //开启事务
            count = houseDao.houseCount();
            tx.commit();        //提交事务
        } catch (HibernateException ex){
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return count;
    }
}
